#include "Plugins/Samples/Common/OrthancPluginCppWrapper.h"
#include "Sources/Logging.h"
#include <string>
#include "ScaleImage.h"
#include <json/json.h>
#include <EmbeddedResources.h>

# define  ORTHANC_PLUGIN_NAME  "MicroPlugin"

void CallbackScaleSeries(OrthancPluginRestOutput *output, const char *url, const OrthancPluginHttpRequest *request) {

    OrthancPluginContext *context = OrthancPlugins::GetGlobalContext();
    LOG(INFO) << "CallbackScaleSeries request received";

    // extract the GET arguments of the request
    if (request->method == OrthancPluginHttpMethod_Get) {

        const char *mySeriesId = request->groups[0];
        LOG(INFO) << " *** mySeriesID " << mySeriesId;

        // get list of instances related to series
        OrthancPluginMemoryBuffer temp;
        std::string seriesUri = "/series/" + std::string(mySeriesId);
        OrthancPluginErrorCode seriesError = OrthancPluginRestApiGetAfterPlugins(context, &temp, seriesUri.c_str());
        LOG(INFO) << "*** seriesError: " << seriesError;

        if (seriesError == 0) {

            // create new SeriesUid
            char *newSeriesUid = OrthancPluginGenerateUuid(context);
            std::string newSeriesUidString = std::string(newSeriesUid);

            // show content of buffer
            const char *seriesJson = static_cast< const char * >(temp.data);
            LOG(INFO) << "JSON Data: " << seriesJson;

            std::string jsonString = std::string(seriesJson);
            Json::Reader reader;
            Json::Value root;
            bool parseStatus = reader.parse(jsonString, root);
            if (parseStatus) {

                const Json::Value &instances = root["Instances"];
                for (const auto &instance: instances) {
                    std::string instanceId = instance.asString();
                    ScaleImage(instanceId, newSeriesUid);
                }

            }

            // free the newSeries Uid
            OrthancPluginFreeString(context, newSeriesUid);
        }
    }

    OrthancPluginAnswerBuffer(context, output, "OK", 2, "text/plain");
}


extern "C"
{
ORTHANC_PLUGINS_API int32_t OrthancPluginInitialize(OrthancPluginContext *context) {

    Orthanc::Logging::InitializePluginContext(context, ORTHANC_PLUGIN_NAME);
    OrthancPluginLogInfo(context, "Storage plugin is initializing");

    OrthancPlugins::SetGlobalContext(context, ORTHANC_PLUGIN_NAME);

    OrthancPluginSetDescription2(context, ORTHANC_PLUGIN_NAME,
                                 "Orthanc Plugin Test with Framework; upload a DICOM file with curl");

    OrthancPlugins::RegisterRestCallback<CallbackScaleSeries>("/series/([^/]+)/scale", true);

    // Extend the default Orthanc Explorer with custom JavaScript
    std::string explorer;
    Orthanc::EmbeddedResources::GetFileResource(explorer, Orthanc::EmbeddedResources::ORTHANC_EXPLORER);
    OrthancPluginExtendOrthancExplorer2(context, ORTHANC_PLUGIN_NAME, explorer.c_str());

    return 0;
}
ORTHANC_PLUGINS_API void OrthancPluginFinalize() {
    LOG(INFO) << ORTHANC_PLUGIN_NAME << "is finalizing";
}
ORTHANC_PLUGINS_API const char *OrthancPluginGetName() {
    return ORTHANC_PLUGIN_NAME;
}
ORTHANC_PLUGINS_API const char *OrthancPluginGetVersion() {
    return "1.0.0";
}
}